<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="plugins/elementui/index.css">
    <link rel="stylesheet" href="plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="css/style.css">
    <!-- 引入组件库 -->
    <script src="plugins/healthmobile.js"></script>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script src="js/vue.js"></script>
    <script src="plugins/elementui/index.js"></script>
    <script src="js/axios-0.18.0.js"></script>
</head>
<body class="hold-transition">
<div id="app"  >
    <!-- 修改密码 -->
    <div class="add-form" >
        <el-dialog title="修改密码" :close-on-click-modal="false" :show-close="false" width="30%" :visible.sync="dialogFormVisible">
            <template>
                <el-tab v-model="activeName" type="card" >
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="20">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="20">
                                <el-form-item label="新密码" prop="password">
                                    <el-input show-password v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="20">
                                <el-form-item label="手机号" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="验证码">
                                    <el-input v-model="formData.code"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10">
                                &nbsp;&nbsp;
                                <el-button :disabled="disabled" @click="sendValidateCode()" type="primary">
                                    {{btn_text}}
                                </el-button>
                            </el-col>
                        </el-row>
                    </el-form>
                </el-tab>
            </template>
            <div slot="footer" class="dialog-footer">
                <el-button @click="backToLogin()">取消</el-button>
                <el-button type="primary" @click="editPassword()">确定</el-button>
            </div>
        </el-dialog>
    </div>
</div>
</body>

<script>
    new Vue({
        el: '#app',
        data: {
            disabled: false,//按钮可选状态
            btn_text: '获取验证码',//按钮内文本
            formData: {},//表单数据
            dialogFormVisible: true,//控制添加窗口显示/隐藏
            rules: {//校验规则
                username: [{required: true, message: '此为必填项', trigger: 'blur'}],
                password: [{required: true, message: '此为必填项', trigger: 'blur'}],
                telephone: [{required: true, message: '此为必填项', trigger: 'blur'}]
            }
        },
        methods: {
            //返回登录页面
            backToLogin() {
                window.location.href = '/login.html';
            },
            //修改密码
            editPassword() {
                axios.post("/user/editPassword.do", this.formData).then((res) => {
                    if (res.data.flag) {
                        window.location.href = '/login.html';
                        alert('修改成功');
                    } else {
                        this.$message.error('修改失败');
                        window.location.href = '/login.html';
                        alert('修改失败');
                    }
                })
            },
            //发送验证码
            sendValidateCode() {
                var telephone = this.formData.telephone;
                var username = this.formData.username;
                if (!checkTelephone(telephone)) {
                    this.$message.error('请输入正确的手机号');
                    return false;
                }

                axios.post("/validateCode/send4EditPwd.do?username=" + username + "&telephone=" + telephone).then((response) => {
                    if (!response.data.flag) {
                        //验证码发送失败
                        this.$message.error('验证码发送失败，用户名或手机号错误');
                        return false;
                    }
                    //计时器
                    let num = 30;
                    clock = window.setInterval(() => {
                        if (num == 0) {
                            this.btn_text = '获取验证码';
                            this.disabled = false;
                            clearInterval(clock);
                        } else {
                            this.disabled = true;
                            this.btn_text = num + 's后重新获取';
                            num--;
                        }
                    }, 1000); //一秒执行一次
                });
            },
        }
    });
</script>
</html>
